/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

import com.cetc.Item;
import com.cetc.Main;
import com.cetc.dao.Dao;
import com.cetc.dao.model.TbGysinfo;
import com.cetc.dao.model.TbKucun;
import com.cetc.dao.model.TbRkthDetail;
import com.cetc.dao.model.TbRkthMain;
import com.cetc.iframe.JinHuoDan.computeInfo;

public class JinHuoTuiHuo extends javax.swing.JInternalFrame {

    /**
     * Creates new form JinHuoTuiHuo
     */
    public JinHuoTuiHuo() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        piaoHao = new javax.swing.JTextField();
        jsfsComboBox = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        gysComboBox = new javax.swing.JComboBox<>();
        thsj = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jsrComboBox = new javax.swing.JComboBox<>();
        lian = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        pzs = new javax.swing.JTextField();
        ysjl = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        hpzs = new javax.swing.JTextField();
        czy = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        hjje = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        sp = new javax.swing.JComboBox<>();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("进货-退货");
        setPreferredSize(new java.awt.Dimension(600, 300));

        jLabel1.setText("退货票号：");
        piaoHao.setEditable(false);

        jLabel2.setText("结算方式：");

        jsfsComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "现金", "支票" }));

        jLabel3.setText("供应商：");
        
        List gysInfos = Dao.getGysInfos();// 获得供应商信息的集合
		for (Iterator iter = gysInfos.iterator(); iter.hasNext();) {// 遍历供应商信息的集合
			List list = (List) iter.next();// 获得迭代的下一个集合
			Item item = new Item();// 数据表公共类
			item.setId(list.get(0).toString().trim());// 编号属性
			item.setName(list.get(1).toString().trim());// 名称信息
			gysComboBox.addItem(item);// 向“供应商”下拉列表中添加选项
		}
        gysComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doGysSelectAction();// “供应商”下拉列表的选择事件
			}
		});

        jLabel4.setText("退货时间：");
        thsj.setEditable(false);
        new Thread(new Runnable() {
			public void run() {
				try {
					while (true) {
						thsjDate = new Date();// 创建日期对象
						thsj.setText(thsjDate.toLocaleString());// 设置“退货时间”文本框中的文本内容
						Thread.sleep(1000);// 线程休眠1秒
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();// 启动线程
        
        jLabel5.setText("联系人：");
        lian.setEditable(false);
        doGysSelectAction();// “供应商”下拉列表的选择事件

        jLabel6.setText("经手人：");
        
        List<List> jsrList = Dao.getJsrs();// 获得被启用的经手人集合
        String[] jsr = new String[jsrList.size()];
        for (int i = 0; i < jsrList.size(); i++) {
        	List list = (List) jsrList.get(i);
    		if (i < jsr.length - 1) {
				jsr[i] = (String) list.get(1) + ",";
			} else {
				jsr[i] = (String) list.get(1);
			}
		}
        jsrComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(jsr));
        
        jLabel7.setText("品种数量：");
        pzs.setEditable(false);

        jLabel8.setText("退货原因：");

        jLabel9.setText("货品总数：");
        hpzs.setEditable(false);

        jLabel10.setText("操作人员：");
        czy.setEditable(false);
        czy.setText(Main.czyStateLabel.getText());

        jLabel11.setText("合计金额：");
        hjje.setEditable(false);

        jButton1.setText("添加");
        
        jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				java.sql.Date date = new java.sql.Date(System.currentTimeMillis());// 使用系统时间值构造一个日期对象
				String maxId = Dao.getRkthMainMaxId(date);// 获取退货最大ID
				piaoHao.setText(maxId);// 设置“退货票号”文本框中的文本内容
				// 结束表格中没有编写的单元
				stopTableCellEditing();
				// 如果表格中不包含空行，就添加新行
				for (int i = 0; i <= jTable1.getRowCount() - 1; i++) {
					if (jTable1.getValueAt(i, 0) == null)
						return;
				}
				DefaultTableModel model = (DefaultTableModel) jTable1.getModel();// 创建表格对象
				model.addRow(new Vector());// 向表格添加空行
				initSpBox();// 初始化商品下拉列表
			}
		});

        jButton2.setText("退货");
        
        jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 停止表格单元的编辑
				stopTableCellEditing();
				// 清除空行
				clearEmptyRow();
				String hpzsStr = hpzs.getText();// 货品总数
				String pzsStr = pzs.getText();// 品种数
				String jeStr = hjje.getText();// 合计金额
				String jsfsStr = jsfsComboBox.getSelectedItem().toString();// 结算方式
				String jsrStr = jsrComboBox.getSelectedItem() + "";// 经手人
				String czyStr = czy.getText();// 操作员
				String rkDate = thsjDate.toLocaleString();// 入库时间
				String ysjlStr = ysjl.getText().trim();// 退货原因
				String id = piaoHao.getText();// 票号
				String gysName = gysComboBox.getSelectedItem().toString();// 供应商名字
				if (jsrStr == null || jsrStr.isEmpty()) {// “经手人”下拉列表不存在或“经手人”下拉列表为空
					JOptionPane.showMessageDialog(JinHuoTuiHuo.this, "请填写经手人");
					return;
				}
				if (ysjlStr == null || ysjlStr.isEmpty()) {// “退货原因”文本框不存在或“退货原因”文本框为空
					JOptionPane.showMessageDialog(JinHuoTuiHuo.this, "填写退货原因");
					return;
				}
				if (jTable1.getRowCount() <= 0) {// 表格模型的行数小于等于0
					JOptionPane.showMessageDialog(JinHuoTuiHuo.this, "填加退货商品");
					return;
				}
				TbRkthMain rkthMain = new TbRkthMain(id, pzsStr, jeStr, ysjlStr, gysName, rkDate, czyStr, jsrStr, jsfsStr);// 进货退货主表
				Set<TbRkthDetail> set = rkthMain.getTbRkthDetails();// 存储进货退货详细信息的集合
				int rows = jTable1.getRowCount();// 获得表格模型中的行数
				for (int i = 0; i < rows; i++) {
					TbKucun kucun = (TbKucun) jTable1.getValueAt(i, 0);// 库存信息
					String djStr = (String) jTable1.getValueAt(i, 6);// 单价
					String slStr = (String) jTable1.getValueAt(i, 7);// 库存数量
					Double dj = Double.valueOf(djStr);// 将String型的单价转换为Double型
					Integer sl = Integer.valueOf(slStr);// 将String型的库存数量转换为Integer型
					TbRkthDetail detail = new TbRkthDetail();// 进货退货详细信息
					detail.setSpid(kucun.getId());// 进货退货编号
					detail.setTbRkthMain(rkthMain.getRkthId());// 进货退货主表
					detail.setDj(dj);// 单价
					detail.setSl(sl);// 数量
					set.add(detail);// 添加进货退货详细信息
				}
				boolean rs = Dao.insertRkthInfo(rkthMain);// 是否成功添加入库信息
				if (rs) {
					JOptionPane.showMessageDialog(JinHuoTuiHuo.this, "退货完成");// 弹出提示框
					DefaultTableModel dftm = new DefaultTableModel();// 创建默认的表格模型对象
					jTable1.setModel(dftm);// 将表格的数据模型设置为dftm
					pzs.setText("0");// 设置“品种数量”文本框中的内容为0
					hpzs.setText("0");// 设置“货品总数”文本框中的内容为0
					hjje.setText("0");// 设置“合计金额”文本框中的内容为0
				}
			}
		});

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                
            },
            new String [] {
            	"商品名称", "商品编号", "产地", "单位", "规格", "包装", "单价", "数量"
            }
        ));
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
		// 添加事件完成品种数量、货品总数、合计金额的计算
        jTable1.addContainerListener(new computeInfo());
        
		sp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TbKucun info = (TbKucun) sp.getSelectedItem();
				if (info != null && info.getId() != null) {// 如果选择有效就更新表格
					updateTable();
				}
			}
		});
		TableColumn column = jTable1.getColumnModel().getColumn(0);// 以表格模型中索引为0的列作参照
		final DefaultCellEditor editor = new DefaultCellEditor(sp);// 创建单元格编辑器
		editor.setClickCountToStart(2);// 开始编辑单元格所需的单击次数为2次
		column.setCellEditor(editor);// 编辑参照列中单元格时所用的编辑器

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(piaoHao, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jsfsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 15, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(thsj, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lian, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jsrComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ysjl, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel10))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pzs, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(czy, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(hpzs, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel11)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)
                                .addGap(18, 18, 18))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(hjje)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(piaoHao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(gysComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lian, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(thsj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(jsrComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jsfsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(pzs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(hpzs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11)
                            .addComponent(hjje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(ysjl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)
                            .addComponent(czy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void doGysSelectAction() {// “供应商”下拉列表的选择事件
		Item item = (Item) gysComboBox.getSelectedItem();// 获得被选中的供应商
		TbGysinfo gysInfo = Dao.getGysInfo(item);// 读取指定供应商信息
		lian.setText(gysInfo.getLian());// 设置“联系人”文本框中的文本内容
	}
    
    private void initSpBox() {// 初始化“商品”下拉列表
		List list = new ArrayList();// 创建存储库存信息的集合
		ResultSet set = Dao.query("select * from tb_kucun where id in(select id from tb_spinfo where gysName='"
				+ gysComboBox.getSelectedItem() + "')");// 获得商品信息的结果集
		sp.removeAllItems();// 移除“商品”下拉列表中的选项
		sp.addItem(new TbKucun());// 向“商品”下拉列表中添加选项
		for (int i = 0; jTable1 != null && i < jTable1.getRowCount(); i++) {
			TbKucun tmpInfo = (TbKucun) jTable1.getValueAt(i, 0);// 获得库存信息
			if (tmpInfo != null && tmpInfo.getId() != null)// 如果库存信息和商品编号都不为空
				list.add(tmpInfo.getId());// 向库存信息的集合中添加商品编号
		}
		try {
			while (set.next()) {// 移动后的记录指针指向一条有效的记录
				TbKucun kucun = new TbKucun();// 库存信息
				kucun.setId(set.getString("id").trim());// 商品编号
				if (list.contains(kucun.getId()))// 如果表格中已存在同样商品，商品下拉框中就不再包含该商品
					continue;
				kucun.setSpname(set.getString("spname").trim());// 商品名称
				kucun.setCd(set.getString("cd").trim());// 产地
				kucun.setJc(set.getString("jc").trim());// 商品简称
				kucun.setDw(set.getString("dw").trim());// 商品计量单位
				kucun.setGg(set.getString("gg").trim());// 商品规格
				kucun.setBz(set.getString("bz").trim());// 包装
				kucun.setDj(Double.valueOf(set.getString("dj").trim()));// 单价
				kucun.setKcsl(Integer.valueOf(set.getString("kcsl").trim()));// 库存数量
				sp.addItem(kucun);// 向“商品”下拉列表中添加库存信息
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
    
    private synchronized void updateTable() {// 根据商品下拉框的选择，更新表格当前行的内容
		TbKucun kucun = (TbKucun) sp.getSelectedItem();// 获得“商品”下拉列表中被选中的选项
		int row = jTable1.getSelectedRow();// 获得表格模型中被选中的行
		if (row >= 0 && kucun != null) {// 表格模型中被选中的行大于等于0且“商品”下拉列表中被选中的选项不为空
			jTable1.setValueAt(kucun.getId(), row, 1);// 商品编号
			jTable1.setValueAt(kucun.getCd(), row, 2);// 产地
			jTable1.setValueAt(kucun.getDw(), row, 3);// 商品计量单位
			jTable1.setValueAt(kucun.getGg(), row, 4);// 商品规格
			jTable1.setValueAt(kucun.getBz(), row, 5);// 包装
			jTable1.setValueAt(kucun.getDj().toString(), row, 6);// 单价
			jTable1.setValueAt(kucun.getKcsl().toString(), row, 7);// 库存数量
			jTable1.editCellAt(row, 7);// 库存数量可编辑
		}
	}
    
    /**
	 * 该方法用于停止表格单元的编辑
	 */
	private void stopTableCellEditing() {
		TableCellEditor cellEditor = jTable1.getCellEditor();// 创建表格单元编辑器
		if (cellEditor != null)// 表格单元编辑器存在
			cellEditor.stopCellEditing();// 停止表格单元的编辑
	}
    
    private synchronized void clearEmptyRow() {// 清除空行
		DefaultTableModel dftm = (DefaultTableModel) jTable1.getModel();// 获得默认的表格模型
		for (int i = 0; i < jTable1.getRowCount(); i++) {
			TbKucun kucun = (TbKucun) jTable1.getValueAt(i, 0);// 获得库存信息
			// 库存信息为空
			if (kucun == null || kucun.getId() == null || kucun.getId().isEmpty()) {
				dftm.removeRow(i);// 移除默认的表格模型中的该行
			}
		}
	}
    
    private final class computeInfo implements ContainerListener {// 在事件中计算品种数量、货品总数、合计金额
		public void componentRemoved(ContainerEvent e) {
			// 清除空行
			clearEmptyRow();
			int rows = jTable1.getRowCount();// 获得表格模型中的行数
			int count = 0;// “货品总数”
			double money = 0.0;// “合计金额”
			TbKucun column = null;// “库存信息”的实例
			if (rows > 0)// 表格模型中的行数大于0
				column = (TbKucun) jTable1.getValueAt(rows - 1, 0);// 为“库存信息”的实例赋值
			if (rows > 0 && (column == null || column.getId().isEmpty()))// 表格模型中的行数大于0且“商品信息”的实例不存在或商品编号为空
				rows--;// 表格模型中的行数减一
			// 计算货品总数和金额
			for (int i = 0; i < rows; i++) {
				String column7 = (String) jTable1.getValueAt(i, 7);// 获得表格中“数量”
				String column6 = (String) jTable1.getValueAt(i, 6);// 获得表格中“单价”
				int c7 = (column7 == null || column7.isEmpty()) ? 0 : Integer.parseInt(column7);// 将String类型的“数量”转换为int型
				Double c6 = (column6 == null || column6.isEmpty()) ? 0 : Double.valueOf(column6);// 将String类型的“单价”转换为Double型
				count += c7;// 计算货品总数
				money += c6 * c7;// 计算合计金额
			}
			pzs.setText(rows + "");// 设置“品种数量”文本框中的文本内容
			hpzs.setText(count + "");// 设置“货品总数”文本框中的文本内容
			hjje.setText(money + "");// 设置“合计金额”文本框中的文本内容
		}

		public void componentAdded(ContainerEvent e) {
		}
	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JinHuoTuiHuo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JinHuoTuiHuo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JinHuoTuiHuo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JinHuoTuiHuo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JinHuoTuiHuo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField czy;
    private javax.swing.JComboBox gysComboBox;
    private javax.swing.JTextField hjje;
    private javax.swing.JTextField hpzs;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox jsfsComboBox;
    private javax.swing.JComboBox jsrComboBox;
    private javax.swing.JTextField lian;
    private javax.swing.JTextField piaoHao;
    private javax.swing.JTextField pzs;
    private javax.swing.JTextField thsj;
    private javax.swing.JTextField ysjl;
	private Date thsjDate;// “退货时间”
	private javax.swing.JComboBox sp;// “商品”下拉列表
    // End of variables declaration//GEN-END:variables
}
